//
//  checkItem.m
//  跨界收
//
//  Created by Allen on 2018/11/1.
//  Copyright © 2018年 gztx. All rights reserved.
//

#import "checkItem.h"

@implementation checkItem

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setup];
    }
    return self;
}
- (void)setup
{
    UILabel *jiaoyiLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 100, 30)];
    jiaoyiLabel.textColor = [UIColor lightGrayColor];
    jiaoyiLabel.text = @"交易类型";
    jiaoyiLabel.font = [UIFont systemFontOfSize:14.0];
    jiaoyiLabel.textAlignment = NSTextAlignmentLeft;
    jiaoyiLabel.tag = 10000;
    [self addSubview:jiaoyiLabel];
    CGFloat width = (SCREEN_WIDTH - 96)/3.0;
    CGFloat distance = 96/4.0;
    _btns = @[@"全部",@"跨界收入",@"提现",@"领红包",@"红包退回",@"发红包",@"奖励"];
    for (int i = 0; i<_btns.count; i++) {
        CGFloat X = distance + (width+distance) * (i%3);
        CGFloat Y =  i/3 *53 + 30;
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(X, Y, width, 43)];
        UILabel *btnLabel = [[UILabel alloc] initWithFrame:CGRectMake(X, Y, width, 43)];
        btnLabel.textColor = [UIColor blackColor];
        btnLabel.textAlignment = NSTextAlignmentCenter;
        btnLabel.tag = 100+i;
        btnLabel.font = [UIFont systemFontOfSize:14];
        btnLabel.text = _btns[i];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 3.0;
        btn.layer.borderWidth = 0;
        btnLabel.hidden = YES;
        [btn setTitle:_btns[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        if (i == 0) {
           [btn setImage:[UIImage imageNamed:@"btn_bg"] forState:UIControlStateNormal];
            btnLabel.hidden = NO;
            btnLabel.backgroundColor = [UIColor clearColor];
        }else{
            btn.backgroundColor = [UIColor cz_colorWithHex:0xEBEBEB];
        }
        btn.tag = 100+i;
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnLabel];
        [self addSubview:btn];
        
    }
}
- (void)click:(UIButton *)sender
{
    for (id btn in self.subviews) {
        if ([btn isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)btn;
            if (button.tag == sender.tag) {
                [button setImage:[UIImage imageNamed:@"btn_bg"] forState:UIControlStateNormal];
                button.backgroundColor = [UIColor clearColor];
            }else{
                [button setTitle:_btns[button.tag-100] forState:UIControlStateNormal];
                [button setImage:nil forState:UIControlStateNormal];
                button.backgroundColor = [UIColor cz_colorWithHex:0xEBEBEB];
            }
        }
        if ([btn isKindOfClass:[UILabel class]]) {
            UILabel *button = (UILabel *)btn;
            if (button.tag == sender.tag) {
                button.hidden = NO;
            }else{
                if (button.tag != 10000) {
                   button.hidden = YES;
                }
            }
        }
    }
    if (self.clickBlock) {
        self.clickBlock(sender);
    }
    
}
@end
